package com.cn.yang.service.impl;

import com.cn.yang.dao.NewsDao;
import com.cn.yang.dao.ProductDao;
import com.cn.yang.dao.impl.NewsDaoImpl;
import com.cn.yang.dao.impl.ProductDaoImpl;
import com.cn.yang.model.news.NewsClassifyModel;
import com.cn.yang.model.news.NewsModel;
import com.cn.yang.model.production.ProductClassifyModel;
import com.cn.yang.service.NewsService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NewsServiceImpl implements NewsService {
    //查询 新闻种类
    @Override
    public List<NewsClassifyModel> findClassify() {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.findClassify();
    }
//添加新闻
    @Override
    public int addNews(NewsModel model) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.addNews(model);
    }
//查询新闻
    @Override
    public List<NewsModel> findNews(NewsModel newsModel) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.findNews(newsModel);
    }
    @Override
    public List<NewsModel> findNews() {
        NewsDao newsDao = new NewsDaoImpl();
        Jedis jedis = new Jedis();
        long length = jedis.llen("news_id");
        List<NewsModel> list = new ArrayList<>();
        if (length>0){
            List<String> news_id = jedis.lrange("news_id",0,-1);
            List<String> news_title = jedis.lrange("news_title",0,-1);
            List<String> news_content = jedis.lrange("news_content",0,-1);
            List<String> news_time = jedis.lrange("news_time",0,-1);
            List<String> news_type = jedis.lrange("news_type",0,-1);
            List<String> news_classifyname = jedis.lrange("news_classifyname",0,-1);
            for (int i =0;i<length;i++){
                NewsModel model = new NewsModel();
                model.setId(Integer.parseInt(news_id.get(i)));
                model.setTitle(news_title.get(i));
                model.setContent(news_content.get(i));
                model.setTime(news_time.get(i));
                model.setType(Integer.parseInt(news_type.get(i)));
                model.setClassifyName(news_classifyname.get(i));
                list.add(model);
            }
        }else {
            list = newsDao.findNews();
            for (int i = 0;i < list.size();i++){
                jedis.rpush("news_id", String.valueOf(list.get(i).getId()));
                jedis.rpush("news_title",list.get(i).getTitle());
                jedis.rpush("news_content",list.get(i).getContent());
                jedis.rpush("news_time",list.get(i).getTime());
                jedis.rpush("news_type",String.valueOf(list.get(i).getType()));
                jedis.rpush("news_classifyname",list.get(i).getClassifyName());
            }
        }
        return list;
    }
//查询总条数
    @Override
    public int countNews() {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.countNews();
    }
//查询一条 id ======
    @Override
    public NewsModel selectById(Integer id) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.selectById(id);
    }
// 更新数据
    @Override
    public int updateNews(NewsModel model) {
        NewsDao newsDao = new NewsDaoImpl();
        Jedis jedis = new Jedis();
        jedis.del("news_id");
        return newsDao.updateNews(model);
    }
//删除数据
    @Override
    public int deleteById(Integer id) {
        NewsDao newsDao = new NewsDaoImpl();
        return newsDao.deleteById(id);
    }
}
